package com.sskj.wallet.ui.activity;


import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.shizhefei.mvc.MVCCoolHelper;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerGridItemDecoration;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.WalletBean;
import com.sskj.lib.model.room.WalletBeanViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.BackupWordActivityPresenter;
import com.sskj.wallet.uitl.TipWalletUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;

@Route(path = RConfig.WALLET_BACKUP_WORD)//备份助记词
public class BackupWordActivity extends BaseActivity<BackupWordActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.btBackup)
    Button btBackup;
    @BindView(R2.id.btShowQR)
    Button btShowQR;
    private SlimAdapter slimAdapter;
    @Inject
    WalletBeanViewModel walletBeanViewModel;
    private WalletBean walletBeanBtc;
    private WalletBean walletBeanEth;
    @Autowired
    String code;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_backup_word;
    }

    @Override
    public BackupWordActivityPresenter getPresenter() {
        return new BackupWordActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.wallet_backupWordActivity1_j));
        DaggerUserDataComponent.create().inject(this);

        ARouter.getInstance().inject(this);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(3, 1, false));
        slimAdapter = SlimAdapter.create().register(R.layout.wallet_recy_item_backup_word, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector, List payloads) {

                injector
                        .text(R.id.tvNum, slimAdapter.getData().indexOf(data) + "")
                        .text(R.id.tvWord, data);
            }
        }).attachTo(recyclerView).updateData(new ArrayList());


    }

    @Override
    protected void initData() {
        walletBeanViewModel.getUsers().observe(this, walletBeans -> {
            if (walletBeans != null) {
                if (walletBeans.get(0).getCode().equals(CoinEnum.BTC.getCode())) {
                    walletBeanBtc = walletBeans.get(0);
                    walletBeanEth = walletBeans.get(1);
                } else {
                    walletBeanBtc = walletBeans.get(1);
                    walletBeanEth = walletBeans.get(0);
                }

                ClickUtil.click(btShowQR, () -> {
                    if (code.equals(CoinEnum.BTC.getCode())) {
                        TipWalletUtil.showQRCode(this, walletBeanBtc.getWords());
                    } else {
                        TipWalletUtil.showQRCode(this, walletBeanEth.getWords());
                    }
                });
                if (code.equals(CoinEnum.BTC.getCode())) {
                    String[] words = walletBeanBtc.getWords().split(" ");
                    List<String> strings = Arrays.asList(words);
                    slimAdapter.updateData(strings);
                    ClickUtil.click(btBackup, () -> {
                        ARouter.getInstance().build(RConfig.WALLET_CHECK_WORD).withString(Constans.WORDS, walletBeanBtc.getWords()).navigation();
                        finish();
                    });
                } else {
                    String[] words = walletBeanEth.getWords().split(" ");
                    List<String> strings = Arrays.asList(words);
                    slimAdapter.updateData(strings);
                    ClickUtil.click(btBackup, () -> {
                        ARouter.getInstance().build(RConfig.WALLET_CHECK_WORD).withString(Constans.WORDS, walletBeanEth.getWords()).navigation();
                        finish();
                    });
                }


            }
        });
    }
}
